/*
 * A simple module to replace Backbone.sync with an interface adapter for the Chrome Bookmarks API. 
 * Models are read from and saved to Chrome's Bookmarks objects.
 */

"use strict";

/* 
 * Override Backbone.sync to use delegate to the model or collection's 
 * bookmarkStorage property, which should be an instance of BookmarkStoreAdapter
 */ 
Backbone.sync = function( method, model, options ) {
  var item
    , store = model.bookmarkStorage || model.collection.bookmarkStorage;
  
  switch( method ) {
    case 'read'   : item = model.id ? store.find( model ) : store.findAll();  break;
    case 'create' : item = store.create( model );                             break;
    case 'update' : item = store.update( model );                             break;
    case 'delete' : item = store.destroy( model );                            break;
  }
  
  if ( item ) options.success( item );
  else options.error( 'Model not found.' ); //TODO: throw exeption?
  
};